<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'

interface Role {
  id: number
  name: string
  description: string
  permissions: string[]
  createTime: string
}

const tableData = ref<Role[]>([
  {
    id: 1,
    name: '超级管理员',
    description: '系统最高权限角色',
    permissions: ['all'],
    createTime: '2024-01-01'
  },
  {
    id: 2,
    name: '普通管理员',
    description: '普通管理权限',
    permissions: ['read', 'write'],
    createTime: '2024-01-02'
  }
])

const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(10)

const dialogVisible = ref(false)
const detailDialogVisible = ref(false)
const dialogTitle = ref('添加角色')
const editingRole = ref<Role>({
  id: 0,
  name: '',
  description: '',
  permissions: [],
  createTime: ''
})
const viewingRole = ref<Role>({
  id: 0,
  name: '',
  description: '',
  permissions: [],
  createTime: ''
})

const permissionOptions = [
  { label: '查看权限', value: 'read' },
  { label: '编辑权限', value: 'write' },
  { label: '删除权限', value: 'delete' },
  { label: '全部权限', value: 'all' }
]

const handleSearch = () => {
  // TODO: 实现搜索功能
}

const handleAdd = () => {
  dialogTitle.value = '添加角色'
  editingRole.value = {
    id: 0,
    name: '',
    description: '',
    permissions: [],
    createTime: new Date().toISOString().split('T')[0]
  }
  dialogVisible.value = true
}

const handleView = (row: Role) => {
  viewingRole.value = { ...row }
  detailDialogVisible.value = true
}

const handleEdit = (row: Role) => {
  dialogTitle.value = '编辑角色'
  editingRole.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = (row: Role) => {
  ElMessageBox.confirm('确定要删除该角色吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      // TODO: 实现删除功能
      ElMessage.success('删除成功')
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

const handleSave = () => {
  if (!editingRole.value.name) {
    ElMessage.warning('请输入角色名称')
    return
  }

  // TODO: 实现保存功能
  ElMessage.success(dialogTitle.value === '添加角色' ? '添加成功' : '更新成功')
  dialogVisible.value = false
}
</script>

<template>
  <div class="roles-container">
    <div class="header">
      <h2>角色管理</h2>
      <div class="actions">
        <el-input v-model="searchQuery"
          placeholder="搜索角色"
          class="search-input"
          @keyup.enter="handleSearch">
          <template #append>
            <el-button @click="handleSearch">
              <el-icon>
                <Search />
              </el-icon>
            </el-button>
          </template>
        </el-input>
        <el-button type="primary"
          @click="handleAdd">添加角色</el-button>
      </div>
    </div>

    <el-table :data="tableData"
      style="width: 100%"
      table-layout="fixed"
      border>
      <el-table-column prop="id"
        label="ID"
        width="80"
        align="center" />
      <el-table-column prop="name"
        label="角色名称"
        width="150" />
      <el-table-column prop="description"
        label="描述"
        min-width="200" />
      <el-table-column label="权限"
        width="200"
        align="center">
        <template #default="{ row }">
          <div class="permissions-container">
            <el-tag v-for="permission in row.permissions"
              :key="permission"
              class="mx-1"
              size="small">
              {{ permission }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime"
        label="创建时间"
        width="180"
        align="center" />
      <el-table-column label="操作"
        width="180"
        align="center"
        fixed="right">
        <template #default="{ row }">
          <el-button link
            type="primary"
            @click="handleView(row)">查看</el-button>
          <el-button link
            type="primary"
            @click="handleEdit(row)">编辑</el-button>
          <el-button link
            type="danger"
            @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="50"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper" />
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog v-model="dialogVisible"
      :title="dialogTitle"
      width="500px">
      <el-form :model="editingRole"
        label-width="100px">
        <el-form-item label="角色名称">
          <el-input v-model="editingRole.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="editingRole.description"
            type="textarea"
            :rows="2" />
        </el-form-item>
        <el-form-item label="权限">
          <el-checkbox-group v-model="editingRole.permissions">
            <el-checkbox v-for="option in permissionOptions"
              :key="option.value"
              :value="option.value">
              {{ option.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary"
            @click="handleSave">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog v-model="detailDialogVisible"
      title="角色详情"
      width="500px">
      <el-descriptions :column="1"
        border>
        <el-descriptions-item label="角色ID">
          {{ viewingRole.id }}
        </el-descriptions-item>
        <el-descriptions-item label="角色名称">
          {{ viewingRole.name }}
        </el-descriptions-item>
        <el-descriptions-item label="描述">
          {{ viewingRole.description }}
        </el-descriptions-item>
        <el-descriptions-item label="权限">
          <el-tag v-for="permission in viewingRole.permissions"
            :key="permission"
            class="mx-1"
            size="small">
            {{ permission }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ viewingRole.createTime }}
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button type="primary"
          @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.roles-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.actions {
  display: flex;
  gap: 10px;
}

.search-input {
  width: 300px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  margin-right: 5px;
}
</style>
